﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

using Cisoft.IntegrationSystem.DataHelper;

namespace Cisoft.Cisoft3G.DAL
{
    /// <summary>
    /// 数据访问类DefineGroupDO。
    /// </summary>
    public class DefineGroupDO
    {
        public DefineGroupDO()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return SQLServerHelper.GetMaxID("DG_ID", "CG_DefineGroup");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int DG_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from CG_DefineGroup");
            strSql.Append(" where DG_ID=@DG_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DG_ID", SqlDbType.Int,4)};
            parameters[0].Value = DG_ID;

            return SQLServerHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Cisoft.Cisoft3G.Model.DefineGroupMO model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CG_DefineGroup(");
            strSql.Append("DG_ID,DG_Name,DG_GroupMember,US_UserID_FK)");
            strSql.Append(" values (");
            strSql.Append("@DG_ID,@DG_Name,@DG_GroupMember,@US_UserID_FK)");
            SqlParameter[] parameters = {
					new SqlParameter("@DG_ID", SqlDbType.Int,4),
					new SqlParameter("@DG_Name", SqlDbType.NVarChar,50),
					new SqlParameter("@DG_GroupMember", SqlDbType.NVarChar,500),
					new SqlParameter("@US_UserID_FK", SqlDbType.Int,4)};
            parameters[0].Value = model.DG_ID;
            parameters[1].Value = model.DG_Name;
            parameters[2].Value = model.DG_GroupMember;
            parameters[3].Value = model.US_UserID_FK;

            SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Cisoft.Cisoft3G.Model.DefineGroupMO model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CG_DefineGroup set ");
            strSql.Append("DG_Name=@DG_Name,");
            strSql.Append("DG_GroupMember=@DG_GroupMember,");
            strSql.Append("US_UserID_FK=@US_UserID_FK");
            strSql.Append(" where DG_ID=@DG_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DG_ID", SqlDbType.Int,4),
					new SqlParameter("@DG_Name", SqlDbType.NVarChar,50),
					new SqlParameter("@DG_GroupMember", SqlDbType.NVarChar,500),
					new SqlParameter("@US_UserID_FK", SqlDbType.Int,4)};
            parameters[0].Value = model.DG_ID;
            parameters[1].Value = model.DG_Name;
            parameters[2].Value = model.DG_GroupMember;
            parameters[3].Value = model.US_UserID_FK;

            SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int DG_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CG_DefineGroup ");
            strSql.Append(" where DG_ID=@DG_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DG_ID", SqlDbType.Int,4)};
            parameters[0].Value = DG_ID;

            SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Cisoft.Cisoft3G.Model.DefineGroupMO GetModel(int DG_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 DG_ID,DG_Name,DG_GroupMember,US_UserID_FK from CG_DefineGroup ");
            strSql.Append(" where DG_ID=@DG_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DG_ID", SqlDbType.Int,4)};
            parameters[0].Value = DG_ID;

            Cisoft.Cisoft3G.Model.DefineGroupMO model = new Cisoft.Cisoft3G.Model.DefineGroupMO();
            DataSet ds = SQLServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["DG_ID"].ToString() != "")
                {
                    model.DG_ID = int.Parse(ds.Tables[0].Rows[0]["DG_ID"].ToString());
                }
                model.DG_Name = ds.Tables[0].Rows[0]["DG_Name"].ToString();
                model.DG_GroupMember = ds.Tables[0].Rows[0]["DG_GroupMember"].ToString();
                if (ds.Tables[0].Rows[0]["US_UserID_FK"].ToString() != "")
                {
                    model.US_UserID_FK = int.Parse(ds.Tables[0].Rows[0]["US_UserID_FK"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DG_ID,DG_Name,DG_GroupMember,US_UserID_FK ");
            strSql.Append(" FROM CG_DefineGroup ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLServerHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" DG_ID,DG_Name,DG_GroupMember,US_UserID_FK ");
            strSql.Append(" FROM CG_DefineGroup ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLServerHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "CG_DefineGroup";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return SQLServerHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法


        public DataSet GetListWithPage(int pageCurrent, int pageSize, string strWhere)
        {
            DataSet ds = new DataSet();
            SqlParameter[] parameters = {
                    new SqlParameter("@tbname", SqlDbType.VarChar,500),
                    new SqlParameter("@FieldKey", SqlDbType.VarChar,500),
                    new SqlParameter("@PageCurrent", SqlDbType.Int,4),
                    new SqlParameter("@PageSize", SqlDbType.Int,4),
                    new SqlParameter("@FieldShow", SqlDbType.VarChar,50),
                    new SqlParameter("@FieldOrder", SqlDbType.VarChar,500),
                    new SqlParameter("@Where", SqlDbType.VarChar,500),
                    new SqlParameter("@ItemCount", SqlDbType.Int,4),
                    new SqlParameter("@PageCount", SqlDbType.Int,4)
                    };
            parameters[0].Value = "CG_DefineGroup";
            parameters[1].Value = "DG_ID";
            parameters[2].Value = pageCurrent;
            parameters[3].Value = pageSize;
            parameters[4].Value = "";
            parameters[5].Value = "DG_ID desc";
            parameters[6].Value = strWhere;
            parameters[7].Direction = ParameterDirection.Output;
            parameters[8].Direction = ParameterDirection.Output;
            ds = SQLServerHelper.ExecuteStoredProcedure("sp_PageView", parameters);
            return ds;
        }


        public int GetListCount(int pageCurrent, int pageSize, string strWhere)
        {
            DataSet ds = new DataSet();
            SqlParameter[] parameters = {
                    new SqlParameter("@tbname", SqlDbType.VarChar,500),
                    new SqlParameter("@FieldKey", SqlDbType.VarChar,500),
                    new SqlParameter("@PageCurrent", SqlDbType.Int,4),
                    new SqlParameter("@PageSize", SqlDbType.Int,4),
                    new SqlParameter("@FieldShow", SqlDbType.VarChar,50),
                    new SqlParameter("@FieldOrder", SqlDbType.VarChar,500),
                    new SqlParameter("@Where", SqlDbType.VarChar,500),
                    new SqlParameter("@ItemCount", SqlDbType.Int,4),
                    new SqlParameter("@PageCount", SqlDbType.Int,4)
                    };
            parameters[0].Value = "CG_DefineGroup";
            parameters[1].Value = "DG_ID";
            parameters[2].Value = pageCurrent;
            parameters[3].Value = pageSize;
            parameters[4].Value = "";
            parameters[5].Value = "DG_ID desc";
            parameters[6].Value = strWhere;
            parameters[7].Direction = ParameterDirection.Output;
            parameters[8].Direction = ParameterDirection.Output;
            object obj = SQLServerHelper.ExecuteStoredProcedure("sp_PageView", parameters, "@ItemCount");
            return Convert.ToInt32(obj);
        }
    }
}

